<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of model_transaksi
 *
 * @author Hadi
 */
class model_transaksi_penyewaan_tc extends Default_My_Model {

    function __construct() {
        parent::__construct();
    }

    public function getPrefixPerjanjianNo() {
        $last_id =  $this->get_scalar("SELECT max(id_perjanjian) as id FROM data_perjanjian_time_charter", "id");
        $number = $last_id+1;
        $prefix = str_pad($number, 5,0,STR_PAD_LEFT);
        return $prefix;
    }

    function getPerjanjianData($whr, $offset = 0, $length= 30) {
        $sql = "SELECT i.*, c.*, b.nama_bank, b.nomor_rekening, b.cabang
                FROM data_perjanjian_time_charter i
                LEFT JOIN master_customer c ON i.id_customer = c.id_customer
                LEFT JOIN master_bank b ON b.id_bank= i.id_bank
                $whr LIMIT $length OFFSET $offset";
        return $this->db->query($sql);
    }

    function deletePerjanjian($kode) {
        $this->db->delete("data_perjanjian_time_charter", "id_perjanjian = '$kode'");
    }

    function insertCustomer($data) {
        $id = $this->namaPelangganExist($data["nama_customer"]);
        if ($id == "") {
            $kontak = explode("/", $data["kontak_komplit"]);
            $alamat = explode("\n", $data["alamat_komplit"]);
            $set = array(
                    "nama_customer" => $data["nama_customer"],
                    "alamat_detail_customer" => $data["alamat_detail_customer"],
                    "attendance" => $data["attendance"] ? $data["attendance"] : "NULL",
                    "telepon_customer" => $kontak[0],
                    "fax_customer" => $kontak[1],
                    "email_customer" => $kontak[2],
                    "alamat_customer" => $alamat[0],
                    "kota_customer" => $alamat[1],
                    "kode_pos_customer" => $alamat[2]
            );
            $this->db->insert("master_customer", $set);
            return $this->db->insert_id();
        }
        return $id;
    }

    function insertVessel($data) {
        $set = array(
                "nama_vessel" => $data["nama_vessel"]
        );
        $this->db->insert("master_vessel", $set);
        return $this->db->insert_id();
    }

    function insertPort($data) {
        $set = array(
                "nama_port" => $data["nama_port"]
        );
        $this->db->insert("master_port", $set);
        return $this->db->insert_id();
    }

    function insertUpdatePerjanjian($data, $id_perjanjian = "") {
        $set = array(
                "tanggal_perjanjian"=>$data["tanggal_perjanjian"],
                "nomor_perjanjian"=>$data["nomor_perjanjian"],
                "penerbit_perjanjian"=>$data["penerbit_perjanjian"],
                "id_customer"=>$data["id_customer"],
                "approved_by"=>$data["approved_by"],
                "nama_jenis_kapal"=>str_replace("\n", "<br>", $data["nama_jenis_kapal"]),
                "jangka_waktu_sewa"=>str_replace("\n", "<br>", $data["jangka_waktu_sewa"]),
                "delivery_dan_redelivery"=>str_replace("\n", "<br>", $data["delivery_dan_redelivery"]),
                "harga_sewa"=>str_replace("\n", "<br>", $data["harga_sewa"]),
                "cara_pembayaran"=>str_replace("\n", "<br>", $data["cara_pembayaran"]),
                "id_bank"=>$data["id_bank"],
                "kewajiban_pemilik"=>str_replace("\n", "<br>", $data["kewajiban_pemilik"]),
                "kewajiban_penyewa"=>str_replace("\n", "<br>", $data["kewajiban_penyewa"]),
                "daerah_operasional_kapal"=>$data["daerah_operasional_kapal"],
                "force_majeure"=>$data["force_majeure"],
                "perselisihan"=>str_replace("\n", "<br>", $data["perselisihan"]),
                "date_submit"=>$data["date_submit"],
                "submitter"=>$data["submitter"],
                "using_dollar_currency"=>$data["using_dollar_currency"],
                "tambahkan_ppn"=>$data["tambahkan_ppn"],
                "diskon"=>$data["diskon"]
        );
        if ($id_perjanjian == "") {
            $this->db->insert("data_perjanjian_time_charter", $set);
            return $this->db->insert_id();
        } else {
            $this->db->update("data_perjanjian_time_charter", $set, "id_perjanjian = '$id_perjanjian'");
        }
    }

    function namaPelangganExist($nama_pelanggan) {
        $id_customer = $this->get_scalar("SELECT id_customer FROM master_customer WHERE nama_customer = '".  addslashes($nama_pelanggan)."'", "id_customer");
        return $id_customer;
    }
}

?>
